package edu.pku.sei.modeler.model.notation.shape;

import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.notation.core.NotationPackage;
import edu.pku.sei.modeler.model.notation.mapping.MappingChain;
import edu.pku.sei.modeler.model.notation.mapping.MappingModel;
import edu.pku.sei.modeler.model.notation.mapping.TextMappingChain;
import edu.pku.sei.modeler.model.xml.XMLUtility;

public class TextObject extends Shape {
	private String text;
	public void setText(String t) {
		String oldText = this.text;
		this.text = t;
		firePropertyChange(NotationPackage.TEXTOBJECT_TEXT, oldText, t);
	}
	@MethodProperty(id="SHAPE_TEXT",catalog="Text",name="Text",setter="setText")
	public String getText() {
		return text;
	}
	@MethodProperty(id="SHAPE_MAPPING",catalog="Text",name="Mapping")
	public String getTextMappingCode(){
		if(textMapping == null)return "";
		MappingChain chain = textMapping.getChain();
		String result = "";
		while(chain != null) {
			if(chain instanceof TextMappingChain) {
				result += "." + ((TextMappingChain)chain).getElementName();
			}
			chain = chain.getNext();
		}
		result = result.substring(1);
		return result;
	}
	private MappingModel textMapping;
	public MappingModel getTextMapping() {
		return textMapping;
	}
	public void setTextMapping(MappingModel textMapping) {
		MappingModel oldTextMapping = this.textMapping;
		this.textMapping = textMapping;
		firePropertyChange(NotationPackage.TEXTOBJECT_TEXTMAPPING, oldTextMapping, textMapping);
	}
	//add by fengchao
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeAttribute(root, NotationPackage.TEXTOBJECT_TEXT, text);
	}
	public void deSerialize(Element root) {
		super.deSerialize(root);
		text = XMLUtility.xmlInstance.deSerializeString(root, NotationPackage.TEXTOBJECT_TEXT);
	}
	//end add
}
